/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.user.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.CookieUtils;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfo.entity.CampusinfoSchool;
import com.lczyfz.campusinfo.modules.campusinfo.service.CampusinfoSchoolService;
import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.sys.security.SystemAuthorizingRealm.Principal;
import com.lczyfz.campusinfo.modules.sys.utils.UserUtils;
import com.lczyfz.campusinfo.modules.user.entity.CampusinfoGroup;
import com.lczyfz.campusinfo.modules.user.service.CampusinfoGroupService;
import com.lczyfz.campusinfo.modules.user.service.CampusinfoUserGroupService;
import com.lczyfz.campusinfo.modules.user.service.CampusinfoUserService;
import com.lczyfz.campusinfo.modules.user.utils.ResponseCode;
import com.lczyfz.campusinfo.modules.user.utils.ServerResponse;
import com.sun.xml.internal.xsom.impl.scd.Iterators.Map;

/**
 * 用户信息Controller
 * @author 陈维乾
 * @version 2018-03-31
 */
@Controller
@RequestMapping(value = "${frontPath}/campusinfo/user")
public class CampusinfoUserApiController extends BaseController {

	@Autowired
	private CampusinfoUserService campusinfoUserService;
	
	//登录用户
	@ResponseBody
	@RequestMapping(value = "login",method = RequestMethod.POST)
	public ServerResponse<String> login(HttpServletRequest request ,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return campusinfoUserService.login(request);
	}
	
	
	
	//注册用户
	@ResponseBody
	@RequestMapping(value = "register",method = RequestMethod.POST)
	public ServerResponse<String> register(User user ,String verifyNum,HttpServletResponse response,HttpServletRequest request){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return campusinfoUserService.register(user,verifyNum,request);
	}
	//短信验证
	@ResponseBody
	@RequestMapping(value = "verify_by_sendmsg",method = RequestMethod.POST)
	public ServerResponse<String> verifyBySendMsg(String mobile,HttpServletRequest request,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return campusinfoUserService.verifyBySendMsg(mobile,request);
	}
	//判读用户名是否存在
	@ResponseBody
	@RequestMapping(value = "is_userName",method = RequestMethod.POST)
	public ServerResponse<String> isUserName(String userName,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return campusinfoUserService.isUserName(userName);
	}
	//判读邮箱是否存在
	@ResponseBody
	@RequestMapping(value = "is_email",method = RequestMethod.POST)
	public ServerResponse<String> isEmail(String email,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return campusinfoUserService.isEmail(email);
	}
	
	//判断是否已登录
	@ResponseBody
	@RequestMapping(value = "has_user",method = RequestMethod.POST)
	public ServerResponse<String> hasUser(HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return campusinfoUserService.hasUser();
	}
	//获取当前用户信息
	@ResponseBody
	@RequestMapping(value = "get_user_info",method = RequestMethod.POST)
	public ServerResponse<User> getUserInfo(HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return campusinfoUserService.getUserInfo();
	}
	
	//忘记密码需要先通过短信验证，然后重新设置密码
	@ResponseBody
	@RequestMapping(value = "forget_reset_password",method = RequestMethod.POST)
	public ServerResponse<String> forgetResetPassword(String loginName,String passwordNew,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return campusinfoUserService.forgetResetPassword(loginName,passwordNew);
	}
	//登录状态重设密码
	@ResponseBody
	@RequestMapping(value = "reset_password",method = RequestMethod.POST)
	public ServerResponse<String> resetPassword(String passwordOld,String passwordNew,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return campusinfoUserService.resetPassword(passwordOld,passwordNew);
	}
	//更新当前用户个人信息
	@ResponseBody
	@RequestMapping(value = "update_information",method = RequestMethod.POST)
	public ServerResponse<User> updateInformation(User user,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return campusinfoUserService.updateInformation(user);
	}
	//判断是否实名认证（姓名，学校，学号，手机号码）
	@ResponseBody
	@RequestMapping(value = "is_verify_user",method = RequestMethod.POST)
	public ServerResponse<String> isVerifyUser(HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return campusinfoUserService.isVerifyUser();
	}
}